package com.android.kotlincoroutinemvvmdemo.view

import android.animation.AnimatorSet
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import android.view.ViewAnimationUtils
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.LinearInterpolator
import com.android.kotlincoroutinemvvmdemo.R

class CircularRevealView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : View(context, attributeSet) {

    private var mWidth = 0
    private var mHeight = 0

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_ff5722)
            style = Paint.Style.FILL
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = when(MeasureSpec.getMode(widthMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(widthMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingStart - paddingEnd

        mHeight = when(MeasureSpec.getMode(heightMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(heightMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingTop - paddingBottom

        setMeasuredDimension(mWidth, mHeight)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            translate(mWidth / 2.0F, mHeight / 2.0F)
            //drawCircle(0F, 0F, mWidth / 3.0F, mPaint0)

            drawRect(
                -mWidth / 2.0F,
                -mHeight / 2.0F,
                mWidth / 2.0F,
                mHeight / 2.0F,
                mPaint0
            )

            createCircularReveal2()
        }
    }

    private fun createCircularReveal() {
        //val animator = ViewAnimationUtils.createCircularReveal(this, 0, 0, mWidth.toFloat(), 0F)
        //animator.duration = 5000
        //animator.start()

        //val delayAnimator = ViewAnimationUtils.createCircularReveal(this, 0, 0, mWidth.toFloat(), mWidth.toFloat())
        //delayAnimator.duration = 300
        //val revealAnimator = ViewAnimationUtils.createCircularReveal(this, 0, 0, mWidth.toFloat(), 0F)
        val revealAnimator =
            ViewAnimationUtils.createCircularReveal(this, 0, 0, mWidth.toFloat() / 3, mWidth.toFloat())
        val animationSet = AnimatorSet()
        //animationSet.playSequentially(delayAnimator, revealAnimator)
        animationSet.interpolator = LinearInterpolator()
        animationSet.play(revealAnimator)
        animationSet.duration = 3000
        animationSet.start()
    }

    private fun createCircularReveal2() {
        val revealAnimator = ViewAnimationUtils.createCircularReveal(this, 0, 0, 0F, mWidth.toFloat())
        //val revealAnimator = ViewAnimationUtils.createCircularReveal(this, 0, 0, mWidth.toFloat(), 0F)
        //revealAnimator.interpolator = LinearInterpolator()
        revealAnimator.interpolator = AccelerateDecelerateInterpolator()
        revealAnimator.duration = 5000
        revealAnimator.start()
    }

}